using System;
using System.Collections;
using System.Reflection;

namespace BEGDC.CC.BL.Workflow
{
	/// <summary>
	/// Summary description for ProcessNodeList.
	/// </summary>
	[Serializable]
	public class ProcessStateList : CollectionBase, System.ICloneable  
	{
		/// <summary>
		/// Constructor
		/// </summary>
		public ProcessStateList() : base()
        {
		}

		/// <summary>
		/// Gets or sets the element at the specified index.
		/// </summary>
		public ProcessState this[ int index ]  
		{
			get  
			{
				return( (ProcessState) List[index] );
			}
			set  
			{
				List[index] = value;
			}
		}

		/// <summary>
		/// Adds an object to the end of the list
		/// </summary>
		/// <param name="value">The Object to be added to the end of the list</param>
		/// <returns>The list index at which the value has been added.</returns>
		public int Add( ProcessState value )  
		{
			return( List.Add( value ) );
		}

		/// <summary>
		/// Searches for the specified process state and returns the zero-based index of the first occurrence within the entire list
		/// </summary>
		/// <param name="value">The process state to locate in the list</param>
		/// <returns>The zero-based index of the first occurrence of value within the entire list, if found; otherwise, -1.</returns>
		public int IndexOf( ProcessState value )  
		{
			return( List.IndexOf( value ) );
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="index"></param>
		/// <param name="value"></param>
		public void Insert( int index, ProcessState value )  
		{
			List.Insert( index, value );
		}

		/// <summary>
		/// Removes the first occurrence of a specific process state from the list.
		/// </summary>
		/// <param name="value">The process state to remove from the list.</param>
		public void Remove( ProcessState value )  
		{
			List.Remove( value );
		}

		/// <summary>
		/// Determines whether the CollectionBase contains a specific process state.
		/// </summary>
		/// <param name="value">The process state to locate in the list. </param>
		/// <returns>true if the list contains the specified value; otherwise, false.</returns>
		public bool Contains( ProcessState value )  
		{
			return( List.Contains( value ) );
		}

		/// <summary>
		/// Performs additional custom processes before inserting a new element into the CollectionBase instance.
		/// </summary>
		/// <param name="index">The zero-based index at which to insert value. </param>
		/// <param name="value">The new value of the element at index. </param>
		protected override void OnInsert( int index, Object value )  
		{
			if ( value.GetType() != typeof(ProcessState) )
				throw new ArgumentException( "value must be of type ProcessState.", "value" );
		}

		/// <summary>
		/// Performs additional custom processes when removing an element from the CollectionBase instance.
		/// </summary>
		/// <param name="index">The zero-based index at which value can be found.</param>
		/// <param name="value">The value of the element to remove from index. </param>
		protected override void OnRemove( int index, Object value )  
		{
			if ( value.GetType() != typeof(ProcessState) )
				throw new ArgumentException( "value must be of type ProcessState.", "value" );
		}

		/// <summary>
		/// Performs additional custom processes before setting a value in the CollectionBase instance.
		/// </summary>
		/// <param name="index">The zero-based index at which oldValue can be found. </param>
		/// <param name="oldValue">The value to replace with newValue.</param>
		/// <param name="newValue">The new value of the element at index. </param>
		protected override void OnSet( int index, Object oldValue, Object newValue )  
		{
			if ( newValue.GetType() != typeof(ProcessState) )
				throw new ArgumentException( "value must be of type ProcessState.", "value" );
		}

		/// <summary>
		/// Performs additional custom processes when validating a value.
		/// </summary>
		/// <param name="value">The object to validate. </param>
		protected override void OnValidate( Object value )  
		{
			if ( value.GetType() != typeof(ProcessState) )
				throw new ArgumentException( "value must be of type ProcessState.", "value" );
		}

		protected ProcessState FindProcessState(string stateKey, ProcessState state)
		{
			if (state.Key == stateKey) return state;
			foreach (ProcessState subState in state.ChildrenStates)
			{
				ProcessState found = FindProcessState(stateKey, subState);
				if (found != null) return found;
			}
			return null;
		}

		public ProcessState GetProcessState(string stateKey)
		{
			foreach (ProcessState state in this)
			{
				ProcessState found = this.FindProcessState(stateKey, state);
				if (found != null) return found;
			}
			return null;
		}

		public ProcessState GetDefaultState()
		{
			if (this.Count > 0) return this[0];
			return null;
		}

		#region ICloneable Members
		/// <summary>
		/// Creates a new object that is a copy of the current instance.
		/// </summary>
		/// <returns>new object</returns>
		public object Clone()
		{
			ProcessStateList newobj = new ProcessStateList();
			for(int i=0; i<this.Count; i++)
			{
				newobj.Add((ProcessState)this[i].Clone());
			}
			return newobj;
		}

		#endregion

//		private int changeid;
//		public int ChangeID
//		{
//			get
//			{
//				return changeid;
//			}
//			set
//			{
//				changeid = value;
//			}
//		}
//
//		private int improvementid;
//		public int ImprovementID
//		{
//			get
//			{
//				return improvementid;
//			}
//			set
//			{
//				improvementid = value;
//			}
//		}
//		private string statusFlag;
//		public string StatusFlag
//		{
//			get
//			{
//				return statusFlag;
//			}
//			set
//			{
//				statusFlag = value;
//			}
//		}
	}
}
